//
//  SLNestedScrollViewController.m
//  Objective-C
//
//  Created by S_LINE on 2019/9/25.
//  Copyright © 2019 S_Line. All rights reserved.
//

#import "SLNestedScrollViewController.h"

#import "SLSwitchView.h"
#import "SLTableView.h"

@interface SLNestedScrollViewController ()<UITableViewDelegate, UITableViewDataSource>


@end

static CGFloat sectionHeaderHeight = 50.0;

@implementation SLNestedScrollViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initView];
}

- (void)initView {
    UITableView *outTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
    self.outTableView = outTableView;
    outTableView.delegate = self;
    outTableView.dataSource = self;
    [self.view addSubview:outTableView];
    
    self.outTableViewOffsetType = SLOffsetType_Zero;
    self.tableView1OffsetType = SLOffsetType_Zero;
    self.tableView2OffsetType = SLOffsetType_Zero;
}

#pragma mark - UITableViewData&UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tableView == self.outTableView) {
        return 2;
    }
    else if (tableView == self.tableView1) {
        return 1;
    }
    else if (tableView == self.tableView2) {
        return 1;
    }
    
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.outTableView) {
        return 1;
    }
    else if (tableView == self.tableView1) {
        return 10;
    }
    else if (tableView == self.tableView2) {
        return 10;
    }

    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    if (tableView == self.outTableView) {
        if (indexPath.section == 0) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"outTableViewSection0"];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"outTableViewSection0"];
            }
            cell.backgroundColor = [UIColor orangeColor];
            return cell;
        }
        else {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"outTableViewSection1"];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"outTableViewSection1"];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            CGFloat containerScrollViewHeight = kScreenHeight - sectionHeaderHeight - kNavigationBarHeight;
            UIScrollView *containerScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, containerScrollViewHeight)];
            containerScrollView.bounces = NO;
            [containerScrollView setContentSize:CGSizeMake(kScreenWidth * 2, containerScrollViewHeight)];
            containerScrollView.pagingEnabled = YES;
            containerScrollView.showsHorizontalScrollIndicator = NO;
            containerScrollView.delegate = self;
            [cell.contentView addSubview:containerScrollView];
            self.containerScrollView = containerScrollView;
            
//            UITableView *tableView1 = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, containerScrollViewHeight) style:UITableViewStylePlain];
//            self.tableView1 = tableView1;
//            tableView1.delegate = self;
//            tableView1.dataSource = self;
//            [containerScrollView addSubview:tableView1];
//
//            UITableView *tableView2 = [[UITableView alloc] initWithFrame:CGRectMake(kScreenWidth, 0, kScreenWidth, containerScrollViewHeight) style:UITableViewStylePlain];
//            self.tableView2 = tableView2;
//            tableView2.delegate = self;
//            tableView2.dataSource = self;
//            [containerScrollView addSubview:tableView2];

            SLTableView *tableView1 = [[SLTableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, containerScrollViewHeight) style:UITableViewStylePlain];
            self.tableView1 = tableView1;
            tableView1.mainVC = self;
//            tableView1.delegate = self;
//            tableView1.dataSource = self;
            [containerScrollView addSubview:tableView1];

            SLTableView *tableView2 = [[SLTableView alloc] initWithFrame:CGRectMake(kScreenWidth, 0, kScreenWidth, containerScrollViewHeight) style:UITableViewStylePlain];
            self.tableView2 = tableView2;
            tableView2.mainVC = self;
//            tableView2.delegate = self;
//            tableView2.dataSource = self;
            [containerScrollView addSubview:tableView2];
            
            return cell;
        }
    }
    else if (tableView == self.tableView1) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableView1Cell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tableView1Cell"];
        }
        cell.textLabel.text = [NSString stringWithFormat:@"tableView1:第%ld行", indexPath.row + 1];
        return cell;
    }
    else if (tableView == self.tableView2) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableView2Cell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tableView2Cell"];
        }
        cell.textLabel.text = [NSString stringWithFormat:@"tableView2:第%ld行", indexPath.row + 1];
        return cell;
    }
    
    return [[UITableViewCell alloc] init];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.outTableView) {
        if (indexPath.section == 0) {
            return 300;
        }
        else {
            return self.containerScrollView.frame.size.height;
        }
    }
    else if (tableView == self.tableView1) {
        return 100;
    }
    else if (tableView == self.tableView2) {
        return 100;
    }

    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (tableView == self.outTableView) {
        if (section == 1) {
            CGRect switchViewFrame = CGRectMake(0, 0, kScreenWidth, sectionHeaderHeight);
            SLSwitchView *switchView = [[SLSwitchView alloc] initWithFrame:switchViewFrame];
            switchView.titles = @[@"第1屏", @"第2屏"];
            UIColor *normalColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
            UIColor *highlightedColor = [UIColor redColor];
            switchView.normalColor = normalColor;
            switchView.highlightedColor = highlightedColor;
            self.switchView = switchView;
            return switchView;
        }
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (tableView == self.outTableView) {
        if (section == 1) {
            return sectionHeaderHeight;
        }
    }
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    if (scrollView == self.containerScrollView) {
        self.tableView1.scrollEnabled = NO;
        self.tableView2.scrollEnabled = NO;
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (scrollView == self.containerScrollView) {
        self.tableView1.scrollEnabled = YES;
        self.tableView2.scrollEnabled = YES;
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.outTableView) {
        if (scrollView.contentOffset.y == 0) {
            self.outTableViewOffsetType = SLOffsetType_Zero;
        }
        else if (scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height - 0.5)) {
            self.outTableViewOffsetType = SLOffsetType_Max;
        }
        else {
            self.outTableViewOffsetType = SLOffsetType_Some;
        }
        
//        if (self.tableView1.offsetType == SLOffsetType_Some && self.switchView.currentIndex == 0) {
        if (self.tableView1OffsetType == SLOffsetType_Some && self.switchView.currentIndex == 0) {

            scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, scrollView.contentSize.height - scrollView.frame.size.height);
        }
        
//        if (self.tableView2.offsetType == SLOffsetType_Some && self.switchView.currentIndex == 1) {
        if (self.tableView2OffsetType == SLOffsetType_Some && self.switchView.currentIndex == 1) {
            scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, scrollView.contentSize.height - scrollView.frame.size.height);
        }
    }
    else if (scrollView == self.containerScrollView) {
        if (scrollView.contentOffset.x < kScreenWidth * 0.5) {
//            [_sectionView updateItemWithIndex:0];
//            _selectedIndex = 0;
        }
        else {
//            [_sectionView updateItemWithIndex:1];
//            _selectedIndex = 1;
        }
    }
    else {
        if (scrollView == self.tableView1) {
            if (scrollView.contentOffset.y <= 0) {
                self.tableView1OffsetType = SLOffsetType_Zero;
            }
            else {
                self.tableView1OffsetType = SLOffsetType_Some;
            }
        }
        else if (scrollView == self.tableView2) {
            if (scrollView.contentOffset.y <= 0) {
                self.tableView2OffsetType = SLOffsetType_Zero;
            }
            else {
                self.tableView2OffsetType = SLOffsetType_Some;
            }
        }

        if (self.outTableViewOffsetType == SLOffsetType_Max) {

        }
        else {
            [scrollView setContentOffset:CGPointZero];
        }
    }
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    NSLog(@"%@", [gestureRecognizer.view class]);
    return YES;
}


@end
